#!/bin/bash
# Test script for enrigindec decoder plugin

# Use installed plugins or local build
if [ -d "../build" ]; then
    export GST_PLUGIN_PATH=$(cd ../build && pwd):$GST_PLUGIN_PATH
    echo "Using local build: $GST_PLUGIN_PATH"
fi

# Test video file
TEST_VIDEO="/root/test_data/video/sample_1080p_h264.mp4"

if [ ! -f "$TEST_VIDEO" ]; then
    echo "Error: Test video not found: $TEST_VIDEO"
    echo "Please provide a test video file"
    exit 1
fi

echo "=== Testing enrigindec Decoder Plugin ==="
echo "Test video: $TEST_VIDEO"
echo ""

# Test 1: Decode MP4 to YUV
echo "Test 1: Decode MP4 to YUV (NV12)"
echo "----------------------------------------"
timeout 10 gst-launch-1.0 \
  filesrc location="$TEST_VIDEO" ! \
  qtdemux ! h264parse ! \
  enrigindec codec=h264 card-id=0 vpu-id=0 ! \
  videoconvert ! video/x-raw,format=NV12 ! \
  filesink location=/tmp/decoded_nv12.yuv

if [ -f "/tmp/decoded_nv12.yuv" ]; then
    SIZE=$(stat -c%s "/tmp/decoded_nv12.yuv")
    echo "✓ Test 1 passed (output: ${SIZE} bytes)"
    rm -f /tmp/decoded_nv12.yuv
else
    echo "✗ Test 1 failed"
fi

echo ""
echo "Test 2: Decode with different VPU"
echo "----------------------------------------"
timeout 10 gst-launch-1.0 \
  filesrc location="$TEST_VIDEO" ! \
  qtdemux ! h264parse ! \
  enrigindec codec=h264 card-id=0 vpu-id=1 ! \
  videoconvert ! \
  filesink location=/tmp/decoded_vpu1.yuv

if [ -f "/tmp/decoded_vpu1.yuv" ]; then
    echo "✓ Test 2 passed (VPU 1 works)"
    rm -f /tmp/decoded_vpu1.yuv
else
    echo "✗ Test 2 failed"
fi

echo ""
echo "Test 3: Full transcode pipeline (decode + encode)"
echo "----------------------------------------"
timeout 15 gst-launch-1.0 \
  filesrc location="$TEST_VIDEO" ! \
  qtdemux ! h264parse ! \
  enrigindec codec=h264 card-id=0 vpu-id=0 ! \
  videoconvert ! video/x-raw,format=NV12 ! \
  enriginenc codec=h264 bitrate=2000 gop-size=30 ! \
  h264parse ! mp4mux ! \
  filesink location=/tmp/transcoded.mp4

if [ -f "/tmp/transcoded.mp4" ]; then
    SIZE=$(stat -c%s "/tmp/transcoded.mp4")
    echo "✓ Test 3 passed (output: ${SIZE} bytes)"
    rm -f /tmp/transcoded.mp4
else
    echo "✗ Test 3 failed"
fi

echo ""
echo "=== All tests completed ==="
